package com.example.main.ukey;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;


public class InputAsyncTask extends AsyncTask<Integer, String, Integer> {

    private static final String TAG = "ImputAsyncTask";

    private OTG_KEY msyUsbKey;
    private Context context;
    private int fingerCnt;
    byte[] fingerBuf = new byte[ZhiWenUtils.IMAGE_X * ZhiWenUtils.IMAGE_Y];

    public InputAsyncTask(Context context, OTG_KEY msyUsbKey, int fingerCnt) {
        this.msyUsbKey = msyUsbKey;
        this.context = context;
        this.fingerCnt = fingerCnt;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.i(TAG, "录入指纹=>开始,请放上手指");
    }

    @Override
    protected Integer doInBackground(Integer... integers) {
        int cnt = 1;
        while (true) {
            int ret;
            while (msyUsbKey.PSGetImage(ZhiWenUtils.DEV_ADDR) != ZhiWenUtils.PS_NO_FINGER) {
                try {
                    Thread.sleep(10);
                } catch (Exception e) {
                }
            }
            while (msyUsbKey.PSGetImage(ZhiWenUtils.DEV_ADDR) == ZhiWenUtils.PS_NO_FINGER) {
                try {
                    Thread.sleep(10);
                } catch (Exception e) {
                }
            }

            //拿指纹图像
            if ((ret = msyUsbKey.PSUpImage(ZhiWenUtils.DEV_ADDR, fingerBuf)) != 0) {
                continue;
            }

            //写指纹图像
            if ((ret = ZhiWenUtils.WriteBmp(context, fingerBuf)) != 0) {
                continue;
            }

            publishProgress("OK");

            //生成模板
            if (cnt == 1) {
                if ((ret = msyUsbKey.PSGenChar(ZhiWenUtils.DEV_ADDR, ZhiWenUtils.CHAR_BUFFER_A)) != ZhiWenUtils.PS_OK) {
                    publishProgress("指纹1生成特征失败:" + ret);
                    continue;
                } else {
                    publishProgress("指纹1生成特征成功 请再次放上手指");
                }
            }

            if (cnt == 2) {
                if ((ret = msyUsbKey.PSGenChar(ZhiWenUtils.DEV_ADDR, ZhiWenUtils.CHAR_BUFFER_B)) != ZhiWenUtils.PS_OK) {
                    publishProgress("指纹2生成特征失败:" + ret);
                    continue;
                } else {
                    publishProgress("指纹2生成特征成功");
                }

                if (msyUsbKey.PSRegModule(ZhiWenUtils.DEV_ADDR) != ZhiWenUtils.PS_OK) {
                    publishProgress("注册指纹模块失败");
                    continue;
                }

                if (msyUsbKey.PSStoreChar(ZhiWenUtils.DEV_ADDR, 1, fingerCnt) != ZhiWenUtils.PS_OK) {
                    publishProgress("存储指纹失败,请重新录入");
                    continue;
                }
                publishProgress("录入指纹成功,=====>ID:" + fingerCnt);
                return 0;
            }

            cnt++;
        }
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        Log.i(TAG, values[0]);
    }

    @Override
    protected void onPostExecute(Integer integer) {
        super.onPostExecute(integer);
        if (fingerCnt > 1000) {
            Log.i(TAG, "指纹存储1000个手指已满，请删除指纹数据");
            return;
        }
    }
}
